package com.dayaokeji.rhythmschoolstudent.client.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.mine.data.AppThemes;
import com.dayaokeji.rhythmschoolstudent.utils.PrefUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Gorden on 2018/3/15.
 */

public class AppThemesAdapter extends BaseAdapter {

    private List<AppThemes.ThemeBean> themeBeans = new ArrayList<>();

    public AppThemesAdapter(List<AppThemes.ThemeBean> themeBeans) {
        this.themeBeans = themeBeans;
    }

    @Override
    public int getCount() {
        return themeBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return themeBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AppThemes.ThemeBean themeBean = themeBeans.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme, null, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.bgView.setBackgroundColor(parent.getContext().getResources().getColor(themeBean.getBgColor()));
        viewHolder.tvThemeName.setText(themeBean.getThemeName());
        Integer chooseThemeType = PrefUtils.getChooseThemeType();
        if (chooseThemeType == themeBean.getType()) {
            viewHolder.ivChooseTheme.setVisibility(View.VISIBLE);
        } else {
            viewHolder.ivChooseTheme.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.bg_view)
        View bgView;
        @BindView(R.id.iv_choose_theme)
        ImageView ivChooseTheme;
        @BindView(R.id.tv_theme_name)
        TextView tvThemeName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
